package tools

import (
	"encoding/json"
	"errors"
	"io/ioutil"
	"reflect"
	"strings"
	"time"

	uuid "github.com/iris-contrib/go.uuid"
	"github.com/sirupsen/logrus"
)

// 生成UUID
func GeneratorUUID() string {
	code, _ := uuid.NewV4()
	return strings.ToLower(strings.Replace(code.String(), "-", "", -1))
}

// GetTime 获取格式化时间文本
func GetTime() string {
	return time.Now().Format("2006-01-02 15:04:05")
}

// 字段检测
func CheckStr(items ...string) error {
	for _, it := range items {
		vt := reflect.TypeOf(it)
		if 0 == len(it) {
			return errors.New(vt.String())
		}
	}
	return nil
}

func ReadFileAsString(filePath string) string {
	f, err := ioutil.ReadFile(filePath)
	if err != nil {
		logrus.Error("can't read this file " + filePath)
		return ""
	}
	return string(f)
}

func InterfaceTransformation(source interface{}, pointer interface{}) error {
	jsonByte, _ := json.Marshal(source)
	return json.Unmarshal(jsonByte, pointer)
}
